package com.cn.delan.web.mvc.admin.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;

import org.json.simple.JSONObject;

import com.cn.delan.core.util.StringUtil;
import com.cn.delan.jdbc.RowList;
import com.cn.delan.web.mvc.admin.bean.UserBean;
import com.cn.delan.web.mvc.core.service.MVCService;
import com.cn.delan.web.mvc.admin.service.IUserService;

public class UserService extends MVCService implements IUserService<UserBean> {

	@Override
	public UserBean findByUserId(String userid) throws Exception{
		String sql = "select `id`,`userid`,`password`,`username`,`superman`,`lock`,`roleid`,`sort`,`lasttime` from sys_user where userid=?";

		UserBean user = null;
		try {
			RowList list = this.getJdbcUtil().query(sql,new Object[]{userid});
			user = list.toClass(UserBean.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}
	@Override
	public UserBean findById(String id) throws Exception{
		String sql = "select `id`,`userid`,`password`,`username`,`superman`,`lock`,`roleid`,`sort`,`lasttime` from sys_user where id=?";

		UserBean user = null;
		try {
			RowList list = this.getJdbcUtil().query(sql,new Object[]{id});
			user = list.toClass(UserBean.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public boolean add(UserBean user) {
		return this.add(user.getUserid(), user.getPassword(), user.getUsername(), user.getRoleid(), user.getSuperman(), user.getLock());
	}

	@Override
	public boolean del(UserBean user) {
		return this.del(user.getId());
	}

	@Override
	public boolean del(String id) {
		String sql = "delete from sys_user where `id`=?";
		try {
			this.getJdbcUtil().update(sql,new Object[]{id});
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean update(UserBean user) {
		return this.update(user.getId(), user.getPassword(), user.getUsername(), user.getRoleid(), user.getSuperman(), user.getLock());
	}

	@Override
	public ArrayList<UserBean> query(String endSQL,Object param[],int pageNo,int pageCount) throws Exception {
		String sql = "select `id`,`userid`,`password`,`username`,`superman`,`lock`,`roleid`,`sort`,`lasttime` from sys_user";
		if(!StringUtil.isEmpty(endSQL)){
			sql += endSQL;
		}
		ArrayList<UserBean>users = null;
		try {
			RowList list = this.getJdbcUtil().query(sql,param);
			users = list.toClassList(UserBean.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return users;
	}

	@Override
	public JSONObject queryToJson(String endSQL, Object[] param,int pageNo,int pageCount) throws Exception {
		String sql = "select `id`,`userid`,`password`,`username`,`superman`,`lock`,`roleid`,`sort`,`lasttime` from sys_user";
		if(!StringUtil.isEmpty(endSQL)){
			sql += endSQL;
		}
		ArrayList<UserBean>users = null;
		JSONObject json = null;
		try {
			RowList list = this.getJdbcUtil().query(sql,param,pageNo,pageCount);
			json = list.toJson();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return json;
	}

	@Override
	public boolean exists(String userId) throws Exception {
		return this.findByUserId(userId)!=null && userId.equals(this.findByUserId(userId).getUserid());
	}

	@Override
	public boolean add(String userid, String password, String username,String roleid, String superman, String lock){
		String id = UUID.randomUUID().toString();
		String sql = "insert into sys_user(`id`,`userid`,`password`,`username`,`roleid`,`superman`,`lock`) values (?,?,?,?,?,?,?)";
		try {
			this.getJdbcUtil().update(sql,new Object[]{id,userid,password,username,roleid,superman,lock});
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean update(String id, String password, String username,String roleid, String superman, String lock) {
		String sql = "update sys_user set `password`=?,`username`=?,`roleid`=?,`superman`=?,`lock`=? where `id`=?";
		try {
			this.getJdbcUtil().update(sql,new Object[]{password,username,roleid,superman,lock,id});
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


}
